package home.study.xxy.bs.controller;

import home.study.xxy.bs.entity.ComJob;
import home.study.xxy.bs.entity.Page;
import home.study.xxy.bs.entity.ResultList;
import home.study.xxy.bs.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @title: JobController
 * @功能:  任务管理接口
 * @Author Xiao Xiangyu
 * @Date: 2021/4/18 15:48
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/jobController")
public class JobController {

    @Autowired
    private JobService jobService;

    /*
    * 查询所有任务
    * */
    @GetMapping("/queryAll")
    ResultList<ComJob> queryAllJob(ComJob comJob,Page pageVo){
        return jobService.queryAllJob(comJob,pageVo);
    }

    /*
    * 查询所有人接的任务数
    * */
    @GetMapping("/queryAllUserJob")
    List queryUserJobCount(){
        return jobService.queryUserJobCount();
    }


    /*
     * 使用id查询接的任务数
     * */
    @GetMapping("/beforeChangeJobState")
    boolean beforeChangeJobState(ComJob comJob){
        if (jobService.queryJobCountById(comJob)>=5){
            return false;
        }else{
            return true;
        }
    }

    /*
     * 使用任务id删除任务
     * */
    @GetMapping("/deleteJob")
    boolean deleteJob(String jobId){
        if (jobService.deleteJob(jobId)==1){
            return false;
        }else{
            return true;
        }
    }

    /*
    * 修改任务状态
    * */
    @GetMapping("/changeJobState")
    boolean changeJobState(ComJob comJob){
        int result = jobService.changeJobState(comJob);
        if (result==1){
            log.info("修改信息成功");
            return true;
        }else if(result < 1){
            log.info("修改信息失败");
            return false;
        }else{
            log.info("修改信息异常");
            return false;
        }
    }

    /*
    * 添加任务
    * */
    @GetMapping("/addJob")
    boolean addJob(ComJob comJob){
        if (comJob!=null){
            comJob.setCreateTime(new Date());
        }
        if (comJob.getJobType()==null || comJob.getJobType().trim().length()<1){
            comJob.setJobType("网站构建");
        }
        jobService.addJob(comJob);
        return true;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

        /**
         * 根据用户id分页查询任务
         * */
    @GetMapping("/queryJobById")
    ResultList<ComJob> queryJobById(ComJob comJob, Page pageVo){
        if (comJob.getUserId()==null){
            log.error("用户id为空查询失败");
            return null;
        }else{

            ResultList<ComJob> jobList = jobService.queryJobById(comJob,pageVo);
           log.info("查询成功");
           if (jobList== null){
               log.error("查询成功，暂无数据");
           }
               return jobList;
        }
    }
    /**
     * 根据接取人id分页查询任务
     * */
    @GetMapping("/queryJobByStaffId")
    ResultList<ComJob> queryJobByStaffId(ComJob comJob, Page pageVo){
        if (comJob.getStaffId()==null){
            log.error("接任务id为空查询失败");
            return null;
        }else{
            ResultList<ComJob> jobList = jobService.queryJobByStaffId(comJob,pageVo);
           log.info("查询成功");
           if (jobList== null){
               log.error("查询成功，暂无数据");
           }
               return jobList;
        }
    }


}
